<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 
      ①dp[i][j]表示长度为i的字符串word1变为长度为j的word2所需要的最少步数
      ②状态转移方程
      总共有三种操作步骤，①删除word1尾部②修改word1尾部③删除word2尾部(等同于添加word1)
      if(word1[i-1]!=word2[j-1])
      那么可以删除word1尾部，继续比较(0,i-2)和(0,j-1) dp[i-1][j]
      该题没修改，修改只能同时删除
      删除word2 dp[i][j-1]

      取min即可
      */
      var minDistance = function (word1, word2) {
        let dp = new Array(word1.length + 1).fill().map(() => new Array(word2.length + 1).fill(0))
        for (let i = 0; i <= word1.length; i++) {
          dp[i][0] = i
        }
        for (let j = 0; j <= word2.length; j++) {
          dp[0][j] = j
        }
        for (let i = 1; i <= word1.length; i++) {
          for (let j = 1; j <= word2.length; j++) {
            if (word1[i - 1] == word2[j - 1]) {
              dp[i][j] = dp[i - 1][j - 1]
            } else {
              //如果不相等，那么就是(0,i-1)与(0,j-2)的操作次数或者是(0,i-2)变到(0,j-1)的操作次数+1
              dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + 1
            }
          }
        }
        return dp[word1.length][word2.length]
      }
    </script>
  </body>
</html>
